package com.ruoyi.stock2.controller;

import java.util.List;

import com.ruoyi.stock2.thread.DownLoadFailThread;
import com.ruoyi.stock2.thread.DownLoadThread;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.stock2.domain.DownTradedataFail;
import com.ruoyi.stock2.service.IDownTradedataFailService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据下载失败Controller
 * 
 * @author ruoyi
 * @date 2020-04-25
 */
@Controller
@RequestMapping("/stock2/FAIL")
public class DownTradedataFailController extends BaseController
{
    private String prefix = "stock2/FAIL";

    @Autowired
    private IDownTradedataFailService downTradedataFailService;

    @RequiresPermissions("stock2:FAIL:view")
    @GetMapping()
    public String FAIL()
    {
        return prefix + "/FAIL";
    }

    /**
     * 查询数据下载失败列表
     */
    @RequiresPermissions("stock2:FAIL:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DownTradedataFail downTradedataFail)
    {
        startPage();
        List<DownTradedataFail> list = downTradedataFailService.selectDownTradedataFailList(downTradedataFail);
        return getDataTable(list);
    }

    /**
     * 导出数据下载失败列表
     */
    @RequiresPermissions("stock2:FAIL:export")
    @Log(title = "数据下载失败", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public String export(DownTradedataFail downTradedataFail)
    {
        DownLoadFailThread obj = DownLoadFailThread.getInstance();
        if(!obj.isWorking()){
            new Thread(obj).start();
        }else {

        }

        return "";
    }

    /**
     * 新增数据下载失败
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存数据下载失败
     */
    @RequiresPermissions("stock2:FAIL:add")
    @Log(title = "数据下载失败", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DownTradedataFail downTradedataFail)
    {
        return toAjax(downTradedataFailService.insertDownTradedataFail(downTradedataFail));
    }

    /**
     * 修改数据下载失败
     */
    @GetMapping("/edit/{symbol}")
    public String edit(@PathVariable("symbol") String symbol, ModelMap mmap)
    {
        DownTradedataFail downTradedataFail = downTradedataFailService.selectDownTradedataFailById(symbol);
        mmap.put("downTradedataFail", downTradedataFail);
        return prefix + "/edit";
    }

    /**
     * 修改保存数据下载失败
     */
    @RequiresPermissions("stock2:FAIL:edit")
    @Log(title = "数据下载失败", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DownTradedataFail downTradedataFail)
    {
        return toAjax(downTradedataFailService.updateDownTradedataFail(downTradedataFail));
    }

    /**
     * 删除数据下载失败
     */
    @RequiresPermissions("stock2:FAIL:remove")
    @Log(title = "数据下载失败", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(downTradedataFailService.deleteDownTradedataFailByIds(ids));
    }
}
